package com.ktwlsoft.archivemanage.domain.business.businessService.customAnalysis;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.customAnalysis.AnalysisDimensionRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.customAnalysis.AnalysisIndexRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.customAnalysis.CustomAnalysisRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.customAnalysis.AnalysisDimension;
import com.ktwlsoft.archivemanage.domain.dao.entity.customAnalysis.AnalysisIndex;
import com.ktwlsoft.archivemanage.domain.dao.entity.customAnalysis.CustomAnalysis;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.customAnalysis.*;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import com.ktwlsoft.archivemanage.service.utils.DateUtils;
import com.ktwlsoft.archivemanage.service.utils.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * 自助分析
 *
 * @author zhuhe on 2019/11/27 15:46
 * @version 1.0
 */
@Component
public class CustomAnalysisService {

    /**自助分析*/
    private final CustomAnalysisRepositoryService analysisRepositoryService;

    /**维度*/
    private final AnalysisDimensionRepositoryService dimensionRepositoryService;

    /**指标*/
    private final AnalysisIndexRepositoryService indexRepositoryService;

    public CustomAnalysisService(CustomAnalysisRepositoryService analysisRepositoryService, AnalysisDimensionRepositoryService dimensionRepositoryService, AnalysisIndexRepositoryService indexRepositoryService) {
        this.analysisRepositoryService = analysisRepositoryService;
        this.dimensionRepositoryService = dimensionRepositoryService;
        this.indexRepositoryService = indexRepositoryService;
    }

    /**
     * 自助分析列表 on 2019/11/27 15:51
     *
     * @param pn pn
     * @param pageSize pageSize
     * @param keyWords keyWords
     * @return Result<?>
     */
    public Result<?> findPage(int pn,int pageSize,String keyWords,String field,String order){
        JSONObject object = new JSONObject();
        object.put("keyWords",keyWords != null && !"".equals(keyWords)?keyWords:null);
        object.put("field",field != null?field.replaceAll("[A-Z]", "_$0").toLowerCase():null);
        object.put("order",order != null?"ascend".equals(order)?"ASC":"DESC":null);
        PageInfo<CustomAnalysis> pageInfo = analysisRepositoryService.findPage(pn,pageSize,object);
        return Result.ok(pageInfo);
    }

    /**
      * 自助分析详情 on 2019/12/3 10:19
      *
      * @param id id
      * @return Result<?>
      */
    public Result<?> getDetails(Long id){
        GetDetailsResult result = analysisRepositoryService.getDetails(id);
        if(result == null){
            return Result.body(id,ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        return Result.ok(result);
    }

    /**
     * 删除图表 on 2019/11/27 16:18
     *
     * @param id id
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> deleteChart(Long id){
        CustomAnalysis customAnalysis = analysisRepositoryService.getById(id);
        if(customAnalysis == null){
            return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        try {
            analysisRepositoryService.deleteById(id);
            dimensionRepositoryService.deleteByAnalysisId(id);
            indexRepositoryService.deleteByAnalysisId(id);
        }catch (Exception e){
            e.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.body(id,ResultType.DELETE_ERROR);
        }
        return Result.ok(id);
    }

    /**
     * 图表新增 on 2019/11/27 16:48
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> save(CustomAnalysisSaveRequest request){
        Account account = UserPrincipal.getCurrentUser();
        if(account == null){
            account = new Account();
        }
        if(request.getDimensionSaves() != null) {
            if(request.getDimensionSaves().size() > 1){
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("维度最大限制值:1");
            }
            if(checkDimension(request.getDimensionSaves()) > 1){
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("维度最大限制值:1");
            }
        }
        if(request.getIndexSaves() != null){
            if(request.getIndexSaves().size() > 5){
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("指标最大限制值:5");
            }
            if(request.getIndexSaves().size() > 1){
                if(request.getDimensionSaves() != null && request.getDimensionSaves().size() >= 2){
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("多维度时指标只能是一个");
                }
            }
            if(checkIndex(request.getIndexSaves()) > 5){
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("指标最大限制值:5");
            }
        }
        CustomAnalysis analysis = new CustomAnalysis();
        BeanUtils.copyProperties(request,analysis);
        analysis.setChartNum(getChartNum());
        analysis.setOperator(account.getRealName());
        analysis.setOperatorId(account.getId());
        analysis.setOperationTime(new Date());
        //维度
        List<AnalysisDimension> dimensions = Lists.newArrayList();
        if(request.getDimensionSaves() != null && request.getDimensionSaves().size() > 0) {
            request.getDimensionSaves().forEach(value ->{
                AnalysisDimension dimension = new AnalysisDimension();
                BeanUtils.copyProperties(value,dimension);
                dimension.setAnalysisId(analysis.getId());
                dimensions.add(dimension);
            });
        }
        //指标
        List<AnalysisIndex> indices = Lists.newArrayList();
        if(request.getIndexSaves() != null && request.getIndexSaves().size() > 0){
            request.getIndexSaves().forEach(value ->{
                AnalysisIndex index = new AnalysisIndex();
                BeanUtils.copyProperties(value,index);
                index.setAnalysisId(analysis.getId());
                indices.add(index);
            });
        }
        try {
            analysisRepositoryService.insert(analysis);
            if(dimensions.size() > 0) {
                dimensionRepositoryService.manyInsert(dimensions);
            }
            if(indices.size() > 0) {
                indexRepositoryService.manyInsert(indices);
            }
        }catch (Exception e){
            e.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.body(null,ResultType.SAVING_ERROR);
        }
        return Result.ok(analysis.getId());
    }

    /**
     * 图表编辑 on 2019/11/27 17:58
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> modify(CustomAnalysisUpdateRequest request){
        Account account = UserPrincipal.getCurrentUser();
        if(account == null){
            account = new Account();
        }
        CustomAnalysis analysis = new CustomAnalysis();
        BeanUtils.copyProperties(request,analysis);
        analysis.setOperator(account.getRealName());
        analysis.setOperatorId(account.getId());
        analysis.setOperationTime(new Date());
        //检查
        List<AnalysisDimension> analysisDimensions = dimensionRepositoryService.getByAnalysisId(analysis.getId());
        List<AnalysisIndex> analysisIndices = indexRepositoryService.getByAnalysisId(analysis.getId());
        List<AnalysisDimensionUpdateRequest> list = Lists.newArrayList();
        if(request.getDimensionUpdates() != null && request.getDimensionUpdates().size() > 0) {
            for (AnalysisDimensionUpdateRequest dimension : request.getDimensionUpdates()) {
                if (dimension.getType().equals(1)) {
                    list.add(dimension);
                }
            }
        }
        if(analysisDimensions != null && analysisDimensions.size() > 0){
            for(AnalysisDimension dimension:analysisDimensions){
                AnalysisDimensionUpdateRequest updateRequest = new AnalysisDimensionUpdateRequest();
                BeanUtils.copyProperties(dimension,updateRequest);
                list.add(updateRequest);
            }
        }
        if(checkUpdateDimension(list) > 1){
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("维度最大限制值:1");
        }
        List<AnalysisIndexUpdateRequest> list1 = Lists.newArrayList();
        if(request.getIndexUpdates() != null && request.getIndexUpdates().size() > 0) {
            for (AnalysisIndexUpdateRequest updateRequest : request.getIndexUpdates()) {
                if (updateRequest.getType().equals(1)) {
                    list1.add(updateRequest);
                }
            }
        }
        if(analysisIndices != null && analysisIndices.size() >= 2){
            for(AnalysisIndex index:analysisIndices){
                AnalysisIndexUpdateRequest updateRequest = new AnalysisIndexUpdateRequest();
                BeanUtils.copyProperties(index,updateRequest);
                list1.add(updateRequest);
            }
        }
        if(checkUpdateIndex(list1) > 5){
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("指标最大限制值:5");
        }
        if(checkUpdateDimension(list) > 1 && checkUpdateIndex(list1) > 1){
            return Result.body(null,ResultType.CHECK_ERROR).withMessage("多维度时指标只能是一个");
        }
        //维度
        List<AnalysisDimension> dimensionsSave = Lists.newArrayList();
        List<AnalysisDimension> dimensionsModify = Lists.newArrayList();
        List<Long> dimensionsDelete = Lists.newArrayList();
        if(request.getDimensionUpdates() != null && request.getDimensionUpdates().size() > 0) {
            request.getDimensionUpdates().forEach(value ->{
                if(!value.getType().equals(3)) {
                    AnalysisDimension dimension = new AnalysisDimension();
                    BeanUtils.copyProperties(value, dimension);
                    dimension.setAnalysisId(analysis.getId());
                    if (value.getType().equals(1)) {
                        //新增
                        dimension.setId(SnowFlake.getNextId());
                        dimensionsSave.add(dimension);
                    }
                    if (value.getType().equals(2)) {
                        //编辑
                        dimensionsModify.add(dimension);
                    }
                }else{
                    //删除
                    dimensionsDelete.add(value.getId());
                }
            });
        }
        //指标
        List<AnalysisIndex> indicesSave = Lists.newArrayList();
        List<AnalysisIndex> indicesModify = Lists.newArrayList();
        List<Long> indicesDelete = Lists.newArrayList();
        if(request.getIndexUpdates() != null && request.getIndexUpdates().size() > 0){
            request.getIndexUpdates().forEach(value ->{
                if(!value.getType().equals(3)) {
                    AnalysisIndex index = new AnalysisIndex();
                    BeanUtils.copyProperties(value, index);
                    index.setAnalysisId(analysis.getId());
                    if(value.getType().equals(1)) {
                        //新增
                        index.setId(SnowFlake.getNextId());
                        indicesSave.add(index);
                    }
                    if(value.getType().equals(2)){
                        //编辑
                        indicesModify.add(index);
                    }
                }else{
                    //删除
                    indicesDelete.add(value.getId());
                }
            });
        }
        try {
            analysisRepositoryService.update(analysis);
            //新增
            if(dimensionsSave.size() > 0) {
                dimensionRepositoryService.manyInsert(dimensionsSave);
            }
            if(indicesSave.size() > 0) {
                indexRepositoryService.manyInsert(indicesSave);
            }
            //编辑
            if(dimensionsModify.size() > 0){
                dimensionsModify.forEach(dimensionRepositoryService::update);
            }
            if(indicesModify.size() > 0){
                indicesModify.forEach(indexRepositoryService::update);
            }
            //删除
            if(dimensionsDelete.size() > 0){
                dimensionRepositoryService.deleteByIds(dimensionsDelete);
            }
            if(indicesDelete.size() > 0){
                indexRepositoryService.deleteByIds(indicesDelete);
            }
        }catch (Exception e){
            e.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.body(null,ResultType.UPDATE_ERROR);
        }
        return Result.ok(analysis.getId());
    }

    /**
     * 获取图表数据 on 2019/11/28 14:16
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> getChartData(GetChartDataRequest request){
        if(request.getDimensionSaves() == null || request.getDimensionSaves().size() <= 0){
            return Result.body(null,ResultType.CHECK_ERROR).withMessage("维度不能为空");
        }
        if(request.getIndexSaves() == null || request.getIndexSaves().size() <= 0){
            return Result.body(null,ResultType.CHECK_ERROR).withMessage("指标不能为空");
        }
        List<JSONObject> retjson = Lists.newArrayList();
        JSONObject object1 = new JSONObject();
        object1.put("selectionDateStart",request.getSelectionDateStart() != null?DateUtils.dayMinTime(DateUtils.dateToStr(request.getSelectionDateStart(),DateUtils.SIMPLESF_PATTERN)):null);
        object1.put("selectionDateEnd",request.getSelectionDateEnd() != null?DateUtils.dayMaxTime(DateUtils.dateToStr(request.getSelectionDateEnd(),DateUtils.SIMPLESF_PATTERN)):null);
        object1.put("guidancePeople",request.getGuidancePeople() != null?request.getGuidancePeople():null);
        object1.put("guidancePeopleId",request.getGuidancePeopleId() != null?request.getGuidancePeopleId():null);
        object1.put("minScore",request.getMinScore() != null?request.getMinScore():null);
        object1.put("maxScore",request.getMaxScore() != null?request.getMaxScore():null);
        for(AnalysisIndexSaveRequest index:request.getIndexSaves()){
            JSONObject object = new JSONObject();
            if(index.getGuidanceScore() != null && index.getGuidanceScore().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //业务检查得分
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getGuidanceScoreData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getGuidanceScoreData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getGuidanceScoreData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getGuidanceScoreData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getGuidanceScoreData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("guidanceScore",list);
            }
            if(index.getMendMark() != null && index.getMendMark().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //整改后得分
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getMendMarkData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getMendMarkData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getMendMarkData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getMendMarkData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getMendMarkData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("mendMark",list);
            }
            if(index.getGuidanceSingleNum() != null && index.getGuidanceSingleNum().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //业务检查单量
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getGuidanceSingleNumData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getGuidanceSingleNumData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getGuidanceSingleNumData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getGuidanceSingleNumData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getGuidanceSingleNumData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("guidanceSingleNum",list);
            }
            if(index.getMendSingleNum() != null && index.getMendSingleNum().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //检查整改单量
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getMendSingleNumData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getMendSingleNumData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getMendSingleNumData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getMendSingleNumData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getMendSingleNumData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("mendSingleNum",list);
            }
            if(index.getArchiveUnitNum() != null && index.getArchiveUnitNum().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //立档单位数量
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getArchiveUnitNumData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getArchiveUnitNumData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getArchiveUnitNumData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getArchiveUnitNumData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getArchiveUnitNumData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("archiveUnitNum",list);
            }
            if(index.getMendNum() != null && index.getMendNum().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //检查整改次数
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getMendNumData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getMendNumData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getMendNumData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getMendNumData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getMendNumData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("mendNum",list);
            }
            if(index.getIndexDefectNum() != null && index.getIndexDefectNum().equals(1)){
                List<JSONObject> list = Lists.newArrayList();
                //指标缺陷次数
                for(AnalysisDimensionSaveRequest dimension:request.getDimensionSaves()){
                    JSONObject object2 = new JSONObject();
                    if(dimension.getDateYear().equals(1)){
                        //时间/年
                        object1.put("type","dateYear");
                        object2.put("dateYear",analysisRepositoryService.getIndexDefectNumData(object1));
                    }
                    if(dimension.getArchiveUnit().equals(1)){
                        //立档单位
                        object1.put("type","archiveUnit");
                        object2.put("archiveUnit",analysisRepositoryService.getIndexDefectNumData(object1));
                    }
                    if(dimension.getGuidancePeople().equals(1)){
                        //指导人
                        object1.put("type","guidancePeople");
                        object2.put("guidancePeople",analysisRepositoryService.getIndexDefectNumData(object1));
                    }
                    if(dimension.getAssessmentIndex().equals(1)){
                        //考核指标
                        object1.put("type","assessmentIndex");
                        object2.put("assessmentIndex",analysisRepositoryService.getIndexDefectNumData(object1));
                    }
                    if(dimension.getGuidanceScoringInterval().equals(1)){
                        //业务检查得分区间
                        object1.put("type","guidanceScoringInterval");
                        object2.put("guidanceScoringInterval",handleData(analysisRepositoryService.getIndexDefectNumData(object1)));
                    }
                    object2.put("sort",index.getSort());
                    list.add(object2);
                }
                object.put("indexDefectNum",list);
            }
            retjson.add(object);
        }
        return Result.ok(retjson);
    }

    /**
     * 处理得分区间数据 on 2019/11/28 17:00
     *
     * @param jsonObjects jsonObjects
     * @return List<JSONObject>
     */
    private List<JSONObject> handleData(List<JSONObject> jsonObjects){
        if(jsonObjects == null){
            return null;
        }
        boolean lt60 = false;
        boolean gt60lt70 = false;
        boolean gt70lt80 = false;
        boolean gt80lt90 = false;
        boolean gt90 = false;
        for(JSONObject object:jsonObjects){
            if("小于60".equals(object.getString("label"))){
                lt60 = true;
            }
            if("大于60小于70".equals(object.getString("label"))){
                gt60lt70 = true;
            }
            if("大于70小于80".equals(object.getString("label"))){
                gt70lt80 = true;
            }
            if("大于80小于90".equals(object.getString("label"))){
                gt80lt90 = true;
            }
            if("大于90".equals(object.getString("label"))){
                gt90 = true;
            }
        }
        List<JSONObject> list = Lists.newArrayList();
        if(!lt60){
            JSONObject object = new JSONObject();
            object.put("label","小于60");
            object.put("num",0);
            list.add(object);
        }else{
            list.add(getLabel(jsonObjects,"小于60"));
        }
        if(!gt60lt70){
            JSONObject object = new JSONObject();
            object.put("label","大于60小于70");
            object.put("num",0);
            jsonObjects.add(object);
        }else{
            list.add(getLabel(jsonObjects,"大于60小于70"));
        }
        if(!gt70lt80){
            JSONObject object = new JSONObject();
            object.put("label","大于70小于80");
            object.put("num",0);
            jsonObjects.add(object);
        }else{
            list.add(getLabel(jsonObjects,"大于70小于80"));
        }
        if(!gt80lt90){
            JSONObject object = new JSONObject();
            object.put("label","大于80小于90");
            object.put("num",0);
            jsonObjects.add(object);
        }else{
            list.add(getLabel(jsonObjects,"大于80小于90"));
        }
        if(!gt90){
            JSONObject object = new JSONObject();
            object.put("label","大于90");
            object.put("num",0);
            jsonObjects.add(object);
        }else{
            list.add(getLabel(jsonObjects,"大于90"));
        }
        return jsonObjects;
    }

    /**
     * 检查维度 on 2019/12/4 10:21
     *
     * @param requestList requestList
     * @return int
     */
    private int checkUpdateDimension(List<AnalysisDimensionUpdateRequest> requestList){
        int i = 0;
        for(AnalysisDimensionUpdateRequest updateRequest:requestList){
            if(updateRequest.getArchiveUnit() != null && updateRequest.getArchiveUnit().equals(1)){
                i++;
            }
            if(updateRequest.getDateYear() != null && updateRequest.getDateYear().equals(1)){
                i++;
            }
            if(updateRequest.getGuidancePeople() != null && updateRequest.getGuidancePeople().equals(1)){
                i++;
            }
            if(updateRequest.getAssessmentIndex() != null && updateRequest.getAssessmentIndex().equals(1)){
                i++;
            }
            if(updateRequest.getGuidanceScoringInterval() != null && updateRequest.getGuidanceScoringInterval().equals(1)){
                i++;
            }
        }
        return i;
    }

    /**
     * 检查指标 on 2019/12/4 10:24
     *
     * @param indexUpdateRequests indexUpdateRequests
     * @return int
     */
    private int checkUpdateIndex(List<AnalysisIndexUpdateRequest> indexUpdateRequests){
        int i = 0;
        for(AnalysisIndexUpdateRequest updateRequest:indexUpdateRequests){
            if(updateRequest.getMendMark() != null && updateRequest.getMendMark().equals(1)){
                i++;
            }
            if(updateRequest.getGuidanceScore() != null && updateRequest.getGuidanceScore().equals(1)){
                i++;
            }
            if(updateRequest.getGuidanceTemScore() != null && updateRequest.getGuidanceTemScore().equals(1)){
                i++;
            }
            if(updateRequest.getGuidanceAverageScore() != null && updateRequest.getGuidanceAverageScore().equals(1)){
                i++;
            }
            if(updateRequest.getGuidanceSingleNum() != null && updateRequest.getGuidanceSingleNum().equals(1)){
                i++;
            }
            if(updateRequest.getMendSingleNum() != null && updateRequest.getMendSingleNum().equals(1)){
                i++;
            }
            if(updateRequest.getArchiveUnitNum() != null && updateRequest.getArchiveUnitNum().equals(1)){
                i++;
            }
            if(updateRequest.getMendNum() != null && updateRequest.getMendNum().equals(1)){
                i++;
            }
            if(updateRequest.getIndexDefectNum() != null && updateRequest.getIndexDefectNum().equals(1)){
                i++;
            }
        }
        return i;
    }

    /**
      * 检查指标 on 2019/12/4 10:24
      *
      * @param indexSaveRequests indexSaveRequests
      * @return int
      */
    private int checkIndex(List<AnalysisIndexSaveRequest> indexSaveRequests){
        int i = 0;
        for(AnalysisIndexSaveRequest saveRequest:indexSaveRequests){
            if(saveRequest.getGuidanceScore() != null && saveRequest.getGuidanceScore().equals(1)){
                i++;
            }
            if(saveRequest.getMendMark() != null && saveRequest.getMendMark().equals(1)){
                i++;
            }
            if(saveRequest.getGuidanceTemScore() != null && saveRequest.getGuidanceTemScore().equals(1)){
                i++;
            }
            if(saveRequest.getGuidanceAverageScore() != null && saveRequest.getGuidanceAverageScore().equals(1)){
                i++;
            }
            if(saveRequest.getGuidanceSingleNum() != null && saveRequest.getGuidanceSingleNum().equals(1)){
                i++;
            }
            if(saveRequest.getMendSingleNum() != null && saveRequest.getMendSingleNum().equals(1)){
                i++;
            }
            if(saveRequest.getArchiveUnitNum() != null && saveRequest.getArchiveUnitNum().equals(1)){
                i++;
            }
            if(saveRequest.getMendNum() != null && saveRequest.getMendNum().equals(1)){
                i++;
            }
            if(saveRequest.getIndexDefectNum() != null && saveRequest.getIndexDefectNum().equals(1)){
                i++;
            }
        }
        return i;
    }

    /**
      * 检查维度 on 2019/12/4 10:21
      *
      * @param saveRequestList saveRequestList
      * @return int
      */
    private int checkDimension(List<AnalysisDimensionSaveRequest> saveRequestList){
        int i = 0;
        for(AnalysisDimensionSaveRequest saveRequest:saveRequestList){
            if(saveRequest.getDateYear() != null && saveRequest.getDateYear().equals(1)){
                i++;
            }
            if(saveRequest.getArchiveUnit() != null && saveRequest.getArchiveUnit().equals(1)){
                i++;
            }
            if(saveRequest.getGuidancePeople() != null && saveRequest.getGuidancePeople().equals(1)){
                i++;
            }
            if(saveRequest.getAssessmentIndex() != null && saveRequest.getAssessmentIndex().equals(1)){
                i++;
            }
            if(saveRequest.getGuidanceScoringInterval() != null && saveRequest.getGuidanceScoringInterval().equals(1)){
                i++;
            }
        }
        return i;
    }

    /**
     * 获取图表编号 on 2019/11/27 16:51
     *
     * @return String
     */
    private String getChartNum(){
        String s = analysisRepositoryService.getChartNum();
        String date = DateUtils.dateToStr(new Date(),"yyyyMMdd");
        if(s == null){
            return date + "001";
        }
        String num = s.substring(s.length() - 3,s.length());
        try {
            num = String.valueOf(Integer.valueOf(num) + 1);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(num.length() == 1){
            return date + "00" + num;
        }
        if(num.length() == 2){
            return date + "0" + num;
        }
        return date + num;
    }

    /**
      * 获取分组数据 on 2019/12/17 15:51
      *
      * @param jsonObjects jsonObjects
      * @param s s
      * @return JSONObject
      */
    private JSONObject getLabel(List<JSONObject> jsonObjects,String s){
        for(JSONObject jsonObject:jsonObjects){
            if(jsonObject.getString("label").equals(s)){
                return jsonObject;
            }
        }
        return new JSONObject();
    }
}